package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.OrderListDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Order;
import com.itheima.service.OrderListService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service(interfaceClass = OrderListService.class)
@Transactional
public class OrderListServiceImpl implements OrderListService {
    @Autowired
    private OrderListDao orderListDao;

    @Override
    public PageResult pageQuery(String[] selectDate, String selectStatus, String selectType, String queryString, Integer pageSize, Integer currentPage) {
        PageHelper.startPage(currentPage, pageSize);
        String startDate = null;
        String endDate = null;
        //截取日期字符串
        if (selectDate != null && selectDate.length > 0) {
            startDate = parseDateZone(selectDate[0]);
            endDate = parseDateZone(selectDate[1]);
        }
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        Page<Map> page = orderListDao.selectByCondition(startDate, endDate, selectStatus, selectType, queryString);
        //处理日期格式
        if (page != null) {
            for (Map pageMap : page) {
                Date orderDate = (Date) pageMap.get("orderDate");
                String date = null;
                try {
                    date = DateUtils.parseDate2String(orderDate);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                pageMap.put("orderDate", date);
            }
        }
        return new PageResult(page.getTotal(), page.getResult());

    }
    //转换日期格式
    public String parseDateZone(String orderDate) {
        orderDate = orderDate.replace("Z", " UTC");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
        SimpleDateFormat defaultFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        String result = null;
        try {
            date = format.parse(orderDate);
            result = defaultFormat.format(date);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //使用开关修改状态
    public void changeStatus(Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        String orderStatus = (String) map.get("orderStatus");
        Order order = orderListDao.findById(id);
        if (order != null) {
            order.setOrderStatus(orderStatus);
            map.put("orderStatus", orderStatus);
            orderListDao.changeStatus(map);
        }
    }

    //删除预约信息
    @Override
    public void delete(Integer id) {
        orderListDao.delete(id);
    }

}
